<?php
//declare (strict_types = 1);
namespace app\admin\controller;
use think\facade\Config;
use app\common\controller\Backend;
use think\facade\Db;
use app\admin\model\Admin;
use app\admin\model\User;
use app\common\model\Attachment;
use app\common\model\ReservationItem;
use app\common\model\Article;
//use app\common\model\Orders;
use fast\Date;

class Dashboard extends  Backend{
    /**
     * @return \think\Response
     */
    public function index(){
        try {
            Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $seventtime = \fast\Date::unixtime('day', -7);
        $paylist = $createlist = [];
        for ($i = 0; $i < 7; $i++) {
            $day = date('Y-m-d', $seventtime + ($i * 86400));
            $createlist[$day] = mt_rand(20, 200);
            $paylist[$day] = mt_rand(1, mt_rand(1, $createlist[$day]));
        }
        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',',
            $hooks['upload_config_init']) : 'local';
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');

        //总订单数
        $totalorder1 =  Db::name('orders') -> count();
        //总收入金额
        $transaction_record = Db::name('user_money_log')  -> field('sum(money) as totalprice') -> select();
        //今日注册
        $dbTableList = Db::query("SHOW TABLE STATUS");
        $this->assign([
            'totaluser'        => User::count(),
            'totalreserve'     => ReservationItem::count(),//总预约单数
            'totalorder'       => $totalorder1,
            'totalorderamount' => $transaction_record[0]['totalprice'],

            'todayusersignup'  => User::whereTime('createtime', 'today')->count(),
            'threednu'         => User::whereTime('createtime', '-3 days')->count(),//3日新增
            'sevendnu'         => User::whereTime('createtime', '-7 days')->count(), //七日新增
            'sevendau'         => User::whereTime('createtime|logintime|updatetime', '-7 days')->count(),//七日活跃
            'todayreserve'     => ReservationItem::whereTime('paytime', 'today')->count(), //今日预约

            'todayorder'       => 2324,//今日订单
            'unsettleorder'    => 132,//未处理订单


            'paylist'          => $paylist,
            'createlist'       => $createlist,
            'addonversion'     => $addonVersion,
            'uploadmode'       => $uploadmode,

            'dbtablenums'     => count($dbTableList),
            'dbsize'          => array_sum(array_map(function ($item) {
                return $item['Data_length'] + $item['Index_length'];
            }, $dbTableList)),

            'totaladmin'      => Admin::count(),

            'thirtydau'       => User::whereTime('createtime|logintime|updatetime', '-30 days')->count(),//月活跃

            'attachmentnums'  => Attachment::count(),
            'attachmentsize'  => Attachment::sum('filesize'),
            'picturenums'     => Attachment::where('mimetype', 'like', 'image/%')->count(),
            'picturesize'     => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),

            'articlecount'       => Article::count(),//
        ]);
//        $this->assignconfig('column', array_keys($userlist));
//        $this->assignconfig('userdata', array_values($userlist));
        return $this ->fetch();
    }

    /**
     * 查看
     */
    public function index1()
    {
        try {
           Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $column = [];
        $starttime = Date::unixtime('day', -6);
        $endtime = Date::unixtime('day', 0, 'end');
        $joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])
            ->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $userlist = array_fill_keys($column, 0);
        foreach ($joinlist as $k => $v) {
            $userlist[$v['join_date']] = $v['nums'];
        }

        $dbTableList = Db::query("SHOW TABLE STATUS");
        $this->view->assign([
            'totaluser'       => User::count(),
            'totaladdon'      => count(get_addon_list()),
            'totaladmin'      => Admin::count(),
            'totalcategory'   => \app\common\model\Category::count(),
            'todayusersignup' => User::whereTime('jointime', 'today')->count(),
            'todayuserlogin'  => User::whereTime('logintime', 'today')->count(),
            'sevendau'        => User::whereTime('jointime|logintime|prevtime', '-7 days')->count(),
            'thirtydau'       => User::whereTime('jointime|logintime|prevtime', '-30 days')->count(),
            'threednu'        => User::whereTime('jointime', '-3 days')->count(),
            'sevendnu'        => User::whereTime('jointime', '-7 days')->count(),
            'dbtablenums'     => count($dbTableList),
            'dbsize'          => array_sum(array_map(function ($item) {
                return $item['Data_length'] + $item['Index_length'];
            }, $dbTableList)),
            'attachmentnums'  => Attachment::count(),
            'attachmentsize'  => Attachment::sum('filesize'),
            'picturenums'     => Attachment::where('mimetype', 'like', 'image/%')->count(),
            'picturesize'     => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),
        ]);

        $this->assignconfig('column', array_keys($userlist));
        $this->assignconfig('userdata', array_values($userlist));

        return  $this  -> fetch();
    }

}
